package com.company.singleton;

/**
 * 懒汉式单例
 * @author HuangZiy
 * @date 2020/09/12
 */
public class Lazy {
    private Lazy(){
        System.out.println(Thread.currentThread().getName()+"OK");
    }

    private static Lazy lazy;
    //双重检测锁模式的，懒汉式单例 DCL懒汉式
    public static Lazy getInstance(){
        if (lazy==null){
            synchronized (Lazy.class){
                if (lazy==null){
                    lazy=new Lazy();
                }
            }
        }
        if (lazy==null){
            lazy=new Lazy();
        }
        return lazy;
    }

    public static void main(String[] args){
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                Lazy.getInstance();
            }).start();
        }
    }
}
